package cn.dglydrpy.study.algorithm.od;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * @author Yyy
 * @date 2024/11/8 17:22
 * @description
 * 模拟目录管理功能，支持创建目录、进入目录、查看当前所在路径等命令，输入一个命令序列，
 * 输出最后一条命令运行结果。约束包括目录名称仅支持小写字母、不支持路径和绝对路径等
 */
public class Od0028_FileManage {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String str = scanner.nextLine();
            String[] inputs = str.split(" ");
            
            myAnswer(inputs);
            answer1(str);
        }
    }
    
    private static final String MKDIR = "mkdir";
    private static final String PWD = "pwd";
    private static final String CD = "cd";
    private static String curPath  = "/";
    private static List<String> validPath = new ArrayList<>();
    private static void myAnswer(String[] inputs) {
        validPath.add("/");
        if(inputs.length == 1){
            if(PWD.equals(inputs[0])){
                // 展示当前路径
                System.out.println(curPath);
            }
            return;
        }

        String cmd = inputs[0];
        String param = inputs[1];
        
        if(MKDIR.equals(cmd)){
            validPath.add((curPath + "/" + param + "/").replace("//","/"));
        }

        if(CD.equals(cmd)){
            if("..".equals(param)){
                int i = curPath.substring(0,curPath.length()-1).lastIndexOf("/");
                curPath = curPath.substring(0,i);
            }else{
                String temp = (curPath + "/" + param + "/").replace("//","/");
                if(validPath.contains(temp)){
                    curPath = temp;
                }
            }
            
        }
        
    }

    private static class Node {
        String name;
        Node parent;
        HashMap<String, Node> child = new HashMap<>();

        Node(String name, Node parent) {
            this.name = name;
            this.parent = parent;
        }
    }

    private static final Node root = new Node("/", null);
    private static Node currNode = root;
    public static void answer1(String str) {
        if (str.startsWith("mkdir ")) {
            // 创建文件
            String input = str.replaceAll("mkdir ", "");
            boolean isRight = isRightInput(input, false);
            if (isRight) {
                if (!currNode.child.containsKey(input)) {
                    currNode.child.put(input, new Node(input, currNode));
                }
            }
        } else if (str.startsWith("cd ")) {
            // 转文件
            String input = str.replaceAll("cd ", "");
            boolean isRight = isRightInput(input, true);
            if (isRight) {
                Node parent = currNode.parent;
                if (input.equals("..")) {
                    // 转到父目录
                    if (parent!= null) {
                        currNode = parent;
                    }
                } else {
                    // 转到子目录
                    if (currNode.child.containsKey(input)) {
                        currNode = currNode.child.get(input);
                    }
                }
            }
        } else if (str.equals("pwd")) {
            // 输出
            String path = "";
            Node node = currNode;
            while (node.parent!= null) {
                path = node.name + "/" + path;
                node = node.parent;
            }
            path = root.name + path;
            System.out.println(path);
        }
    }

    // 输入是否为有效字符串
    public static boolean isRightInput(String inputStr, boolean isCd) {
        boolean isRight = true;
        int length = inputStr.length();
        if (length == 0) return false;
        if (isCd) {
            if (inputStr.contains(".")) {
                if (inputStr.equals("..")) {
                    return true;
                } else return false;
            }
            if (length > 10) return false;
            for (int i = 0; i < length; i++) {
                char value = inputStr.charAt(i);
                if (value < 'a' || value > 'z') {
                    return false;
                }
            }
        }
        return isRight;
    }
}
